<#assign tag = fn.tag><#t>
<#if _topPage?exists && _topPage><#assign tmp = ps.safeContext().setTopPage(true)></#if><#t>
<#if !_context?exists><#assign _context = ps.safeContext()></#if><#t>

<#--
	@Private
-->
<#macro inject name='' type='' point='' once=false priority=10>
	<#if _context.enableInject(name, type, point, once) || fn.isDebug()>
		<#local oldMock = ps.setMock(false)>
		<#assign x><#nested></#assign>
		<#local oldMock = ps.setMock(oldMock)>
		<#local tmp = _context.inject(name, type, point, once, priority, x)>
	</#if>
</#macro>

<#--
	@Private
-->
<#macro header topPage=false ignore=false>
	<#if _context?exists>
		<#local tmp = _context.setTopPage(topPage)>
		<#if ignore>
			${_context.setProcessed(true)}<#t>
		<#else>
			${_context.buildHeader()}
		</#if>
	</#if>
</#macro>

<#--
	@Private
-->
<#macro footer>
	<#if _context?exists>${_context.buildFooter()}</#if>
</#macro>

<#macro include url name=''>
	<#local shortName = tag.getShortName(name)>
	<#local _pageElement = ps.checkAddElement(shortName, name, 'include', {'url':url})>
	
	<#local isMock = ps.isMock()>
	<#if isMock>
		<div name='${shortName}' fullName='${name}' class="jinclude stdEle refEle" uiType='include'>
	</#if>
	
	<#if url != '' && !ps.isDebug()>
		<#if url?starts_with('/') && url?ends_with('.ftl')>
			<#attempt>
				<#local url = tag.getIncludeUrl(url, isMock)>
				<#if url?length gt 0>
					<#include url>
				</#if>
			<#recover>
				Wrong page: ${url}, <font color="red">${.error}</font>
			</#attempt>
		<#else>
			${tag.include(url)}
		</#if>
	</#if>
	
	<#if isMock>
		</div>
	</#if>
</#macro>

<#-- PageConverterService & TagSectionElementAdaptor processes it automatically-->
<#macro code value=''>
</#macro>

<#macro css src='' prefix='' async=true name='' _inner=false _priority=20>
	<#local shortName = tag.getShortName(name)>
	<#if shortName != ''>
		<#local _pageElement = ps.getElement(shortName, false)!false>
		<#if _pageElement?is_boolean>
			<#local _pageElement = ps.addElement('css', shortName, '', false, false, 0, true)>
			<#local tmp = _pageElement.addExtraParams('src', src)>
			<#local tmp = _pageElement.addExtraParams('prefix', prefix)>
		</#if>
	</#if>
	
	<#local fullSrc = tag.getCssPath(ps, prefix, src)>
	<#local isMock = ps.isMock()>
	<#if isMock && !_inner><div name='${shortName}' fullName='${name}' class="jcss stdEle refEle" uiType='css' src="${fullSrc}"></#if>
		<#if src !=''>
			<#--TODO: disable async parameter -->
			<#local topPage = _context.isTopPage()>
			<#if !async>
				<link href="${fullSrc}" type="text/css" rel="stylesheet"/>
			<#elseif topPage>
				<#assign x><link href="${fullSrc}" type="text/css" rel="stylesheet"/></#assign>
				<#local tmp = _context.deject(fullSrc, 'script', 'header')>		<#-- remove the injection of '<@script>' -->
				<#local tmp = _context.inject(fullSrc, 'html', 'header', false, _priority, x)>
			<#else>
				<#if _context.enableInject(fullSrc, 'html', 'header', true)>	<#-- 'if topPage' has higher priority-->
					<@script name=fullSrc header=true _inner=true>getCss("${fullSrc}");</@script>
				</#if>
			</#if>
		</#if>
	<#if isMock && !_inner></div></#if>
</#macro>

<#macro script src='' async=true name='' header=false runCacheJs=false _inner=false _priority=20>
	<#local shortName = tag.getShortName(name)>
	<#if shortName != ''>
		<#local _pageElement = ps.getElement(shortName, false)!false>
		<#if _pageElement?is_boolean>
			<#local _pageElement = ps.addElement('script', shortName, '', false, false, 0, true)>
			<#local tmp = _pageElement.addExtraParams('src', src)>
		</#if>
	</#if>
	
	<#local fullSrc = tag.getScriptPath(src, ps.isMock())>
	<#local isMock = ps.isMock()>
	<#if isMock && !_inner><div name='${shortName}' fullName='${name}' class="jscript stdEle refEle" uiType='script' src="${fullSrc}" async="${async?string}"></#if>
		<#if !ps.isDebug()>
			<#if src != ''>
				<#if async>
					<#assign x>getScript("${fullSrc}", ${runCacheJs?string});</#assign>
					<#if header><#local pointer='header'><#else><#local pointer='footer'></#if>
					<#local tmp = _context.inject(fullSrc, 'script', pointer, false, _priority, x)>
				<#else>
					<script name='${name}' src="${fullSrc}" type="text/javascript"></script>
				</#if>
			<#else>
				<#if async>
					<#assign x><#nested></#assign>
					<#if header><#local pointer='header'><#else><#local pointer='footer'></#if>
					<#local tmp = _context.inject(name, 'script', pointer, false, _priority, x)>
				<#else>
					<script type="text/javascript"><#nested></script>
				</#if>
			</#if>
		</#if>
	<#if isMock && !_inner></div></#if>
</#macro>

<#macro layout name='' clazz='' style='' id='' freeModel=false positionCol=-1>
	<div<#if id!=''> id="${id}"</#if> name="${tag.getShortName(name)}" fullName="${name}" class="jlayout stdEle stdCtn ${clazz}<#if freeModel> freeCtn</#if> ctnEle"
		<#if positionCol != -1> positionCol=${positionCol}</#if> uiType="layout"<#if style!=''> style="${style}"</#if>>
		<#nested>
	</div>
</#macro>

<#-- ConversionCallback.parseTemplateTag Process it automatically -->
<#macro template url='' style='' templateContainTagInfo=false>
	<#nested>
</#macro>

<#macro datasource name='' collection=[] var='item' style=''>
	<#local shortName = tag.getShortName(name)>
	<#local isMock = ps.isMock()>
	<#local _ps = ps>
	<#assign ps = ps.checkAddElement(shortName, name, 'datasource', {})>
	
	<#if !collection?is_sequence && collection?is_string>
		<#local collection = fn.val(.vars, collection)![]>
	</#if>
	<#if collection?size == 0>
		<#if isMock>
			<#local collection = ['a']>
		</#if>
	</#if>
	
	<#if isMock><span name="${name}" uiType="datasource" class="ctnEle stdEle stdCtn" style="${style}"></#if>
	<#if collection?size gt 0>
		<#list collection as item1>
			<#-- assign value 'item1' to variable 'var', for example, item = item1, see BodyInstruction.java -->
			<#nested assign, var, item1>
			<#-- inject each property of item to the first layer Macro invocation, see BodyInstruction.java -->
			<#nested inject, item1>
		</#list>
	</#if>
	<#if isMock></span></#if>
	
	<#assign ps = _ps>
</#macro>

<#macro condition name='' test=true style=''>
	<#local isMock = ps.isMock()>
	<#if isMock><span name="${name}" uiType="condition" class="ctnEle stdEle stdCtn" style="${style}"></#if>
	<#if test>
		<#nested>
	</#if>
	<#if isMock></span></#if>
</#macro>

<#macro form name='' action='' enctype='' skipForm=true from='' clazz='' id='' validator=true includeHf=true>
	<#local cid = tag.getCid(name, 'form')>
	<#local shortName = tag.getShortName(name)>
	<#if skipForm>
		<div<#if id!=''> id="${id}"</#if> name="${shortName}" fullName="${name}" class="jform ${clazz} ${cid}" action='<#if action!=''>${fn.link(action)}</#if>'
			<#if from!=''> from="${fn.link(from)}"</#if>><#lt>
			<#nested>
		</div>
	<#else>
		<form<#if id!=''> id="${id}"</#if> name="${shortName}" fullName="${name}" class="jform ${clazz} ${cid}" action='<#if action!=''>${fn.link(action)}</#if>' enctype='${enctype}' method='post'
			<#if from!=''> from="${fn.link(from)}"</#if>><#lt>
			<#nested>
		</form>
	</#if>
	<#if validator>
	<@script _inner=true>
		when('initValidate', function() {
			initValidate($('.${cid}'));
		});
	</@script>
	</#if>
</#macro>